package com.rules.admin.controller;


import com.rules.admin.entity.PermissionReference;
import com.rules.admin.service.PermissionService;
import com.rules.admin.service.RulePackageService;
import com.rules.admin.utils.ResultUtils;
import com.rules.admin.vo.UserGroupPermissionInfo;
import com.rules.admin.vo.UserPermissionInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping({"/permissions"})
@Controller
public class PermissionController {

   @Autowired
   private PermissionService permissionService;

   @Autowired
   private RulePackageService rulePackageService;

   @RequestMapping(
      value = {"/{targetId}/{targetType}/{mode}/users"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UserPermissionInfo> getAllUserPermissionInfos(@PathVariable("targetId") Long targetId, @PathVariable("targetType") Integer targetType, @PathVariable("mode") Integer mode, String username) {
      username = StringUtils.isEmpty(username) ? "" : username;
      return this.rulePackageService.selectUserPermissionInfos(targetId, targetType, mode, "%" + username + "%");
   }

   @RequestMapping(
      value = {"/{targetId}/{targetType}/{mode}/userGroups"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UserGroupPermissionInfo> getAllUserGroupPermissionInfos(@PathVariable("targetId") Long targetId, @PathVariable("targetType") Integer targetType, @PathVariable("mode") Integer mode, String groupName) {
      groupName = StringUtils.isEmpty(groupName) ? "" : groupName;
      return this.rulePackageService.selectUserGroupPermissionInfos(targetId, targetType, mode, "%" + groupName + "%");
   }

   @RequestMapping(
      value = {"/{targetId}/{targetType}/{mode}/users"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createUserPermissionInfo(@RequestBody PermissionReference permissionReference, Integer model) {
      this.permissionService.save(permissionReference);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{targetId}/{targetType}/{mode}/users/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteUserPermissionInfo(@PathVariable("id") Long id) {
      this.permissionService.removeById(id);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{targetId}/{targetType}/{mode}/userGroups"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createUserGroupPermissionInfo(@RequestBody PermissionReference permissionReference) {
      this.permissionService.save(permissionReference);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{targetId}/{targetType}/{mode}/userGroups/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteUserGroupPermissionInfo(@PathVariable("id") Long id) {
      this.permissionService.removeById(id);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/getPermissions"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getPermissions(Long packageId) {
      return ResultUtils.getSuccessResultData(this.permissionService.getPermissions(packageId));
   }

   @RequestMapping(
      value = {"/permissionsJudgment"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> permissionsJudgment(Long packageId) {
      return ResultUtils.getSuccessResultData(this.permissionService.permissionsJudgment(packageId));
   }
}
